/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.example;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringSystem;
import com.tumblr.backboard.Utils.MathUtils;
import com.tumblr.backboard.Utils.View;
import com.tumblr.backboard.performer.Performer;
import ohos.agp.components.Component;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Scale an view with an finger.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class ScaleFragment extends BaseFraction {
    private float mDownX = 0f;

    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_scale;
    }

    @Override
    public void initComponent() {
        Component rect = mComponent.findComponentById(ResourceTable.Id_rect);

        SpringSystem springSystem = SpringSystem.create();
        Spring spring = springSystem.createSpring();
        spring.addListener(new Performer(rect, View.SCALE_X));
        spring.addListener(new Performer(rect, View.SCALE_Y));

        mComponent.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int[] rectLocation = rect.getLocationOnScreen();
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        mDownX = touchEvent.getPointerPosition(touchEvent.getIndex()).getX();
                        break;
                    case TouchEvent.POINT_MOVE:
                        // can't use Imitation here because there is no nice mapping from an event property to Spring
                        float moveX = touchEvent.getPointerPosition(touchEvent.getIndex()).getX();
                        float moveY = touchEvent.getPointerPosition(touchEvent.getIndex()).getY();
                        float delta = MathUtils.floatToSubtract(moveX,
                                (float) (rectLocation[0] + rect.getWidth() / VALUE_TWO));
                        float scaleX = Math.abs(delta) / ((float) rect.getWidth() / (float) VALUE_TWO);
                        delta = MathUtils.floatToSubtract(moveY,
                                (float) (rectLocation[1] + rect.getHeight() / VALUE_TWO));
                        float scaleY = Math.abs(delta) / ((float) rect.getHeight() / (float) VALUE_TWO);
                        float scale = Math.max(scaleX, scaleY) / (float) VALUE_TWO;
                        if (mDownX != moveX) {
                            spring.setEndValue(scale);
                        }
                        break;
                    case TouchEvent.PRIMARY_POINT_UP:
                        spring.setEndValue(1.0f);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
}
